package com.cloudeasy.post.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudeasy.api.util.SnowFlake;
import com.cloudeasy.api.vo.post.PushVO;
import com.cloudeasy.api.vo.post.SimpleUserInfoVO;
import com.cloudeasy.post.dao.mapper.StoreyMapper;
import com.cloudeasy.post.dto.ReviewDTO;
import com.cloudeasy.post.po.Storey;
import com.cloudeasy.post.service.expand.PushService;
import com.cloudeasy.post.service.expand.SimpleUserInfoService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class StoreyService extends ServiceImpl<StoreyMapper, Storey> {

    private final ReplyService replyService;

    private final SimpleUserInfoService simpleUserInfoService;

    private final SnowFlake snowFlake;

    private final PushService pushService;

    public StoreyService(@Lazy ReplyService replyService, @Lazy SimpleUserInfoService simpleUserInfoService, SnowFlake snowFlake, PushService pushService) {
        this.replyService = replyService;
        this.simpleUserInfoService = simpleUserInfoService;
        this.snowFlake = snowFlake;
        this.pushService = pushService;
    }

    /**
     * 根据评论Id列表删除记录
     *
     * @param replyIds 回复Id列表
     * @return {@link Boolean}
     */
    public Boolean removeByReplyIds(List<String> replyIds) {
        if (replyIds.size() == 0) {
            return true;
        }
        QueryWrapper<Storey> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("reply_id", replyIds);
        return this.remove(queryWrapper);
    }

    /**
     * 获取前两条记录
     *
     * @param replyId 评论Id
     * @return {@link IPage<Storey>}
     */
    public IPage<Storey> simpleListByReplyId(String replyId) {
        QueryWrapper<Storey> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("reply_id", replyId)
                .orderByAsc("reply_time");
        IPage<Storey> storeyIPage = new Page<>();
        storeyIPage.setCurrent(1).setSize(2);
        return this.page(storeyIPage, queryWrapper);
    }

    /**
     * 根据评论Id查询回复
     *
     * @param replyId 评论Id
     * @return {@link List<Storey>}
     */
    public List<Storey> listByReplyId(String replyId) {
        QueryWrapper<Storey> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("reply_id", replyId)
                .orderByAsc("reply_time");
        return this.list(queryWrapper);
    }

    /**
     * 回复
     *
     * @param replyId    评论Id
     * @param answererId 回复者Id
     * @param reviewDTO  {@link ReviewDTO}
     * @return {@link Boolean}
     */
    public Boolean save(String replyId, String answererId, ReviewDTO reviewDTO) {
        String reviewerId = replyService.answer(replyId);
        // 推送
        SimpleUserInfoVO answerer = simpleUserInfoService.obtainSimpleUserInfo(Collections.singletonList(answererId)).get(0);
        PushVO pushVO = PushVO.builder()
                .message(String.format("%s 回复了您的评论", answerer.getNickname()))
                .responseId(replyId)
                .responder(answerer)
                .type(1)
                .pushDate(new Date())
                .build();
        pushService.push(reviewerId, pushVO);
        String id = String.valueOf(snowFlake.nextId());
        Storey storey = Storey.builder()
                .id(id)
                .replyId(replyId)
                .answererId(answererId)
                .content(reviewDTO.getContent())
                .build();
        return this.save(storey);
    }
}
